package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-03-02
 */
@Api(description="添加小节、删除小节")
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节
    // TODO 后面这个方法需要完善：删除小节时候，同时把里面视频删除
    @ApiOperation(value = "删除小节")
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id) {
        //完善
        //删除小节传入的是小节id，要根据小节id获取到视频id，进行删除视频
        EduVideo eduVideo = videoService.getById(id);
        System.out.println("eduVideo看看是什么："+eduVideo);
        //videoSourceId==>云端视频id，用服务调用把视频id发到service_vod的删除视频中
        String videoSourceId = eduVideo.getVideoSourceId();
        System.out.println("视频id："+videoSourceId);
        //判断视频id是否为空
        //这里按照老师写删小节会报错，要用StringUtils.isEmpty(eduVideo.getVideoSourceId())
        if(!StringUtils.isEmpty(eduVideo.getVideoSourceId())){
            System.out.println("看看能不能删除视频");
            //如果不等于空，删除
            vodClient.removeAiyVideo(videoSourceId);
            //测试
            /*R r = vodClient.removeAiyVideo(videoSourceId);
            if (r.getCode() == 20001){
                throw new GuliException(20001,"删除视频失败，熔断器...");
            }*/
        }

        videoService.removeById(id);
        return R.ok();
    }

    //修改小节 TODO

}

